package cn.iocoder.yudao.module.design.service.user;

import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;

/**
 * @author Annie
 * @ClassName DesignUserServcieImpl
 * @description: TODO
 * @date 2024年06月05日
 * @version: 1.0
 */
@Service
@Validated
@Slf4j
public class DesignUserServcieImpl implements DesignUserService{

    @Resource
    private AdminUserMapper userMapper;

    @VisibleForTesting
    void validateUserExists(Long id) {
        if (id == null) {
            return;
        }
        AdminUserDO user = userMapper.selectById(id);
        if (user == null) {
            throw exception(USER_NOT_EXISTS);
        }
    }
    @Override
    public void updateUserInfo(Long id, String nickname, String avatar) {
        // 校验用户存在
        validateUserExists(id);
        // 更新昵称
        AdminUserDO updateObj = new AdminUserDO();
        updateObj.setId(id);
        updateObj.setAvatar(avatar);
        updateObj.setNickname(nickname);
        userMapper.updateById(updateObj);
    }
}
